//
//  UserFollowerTableHandler.m
//  collection
//
//  Created by 靖 张 on 12/13/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "UserFollowerTableHandler.h"
#import "UserProfileController.h"
#import "CollectionDataManager.h"
#import "UserRelationCell.h"

@interface UserFollowerTableHandler()

@property (nonatomic, strong)   NSMutableArray* followerList;

@end

@implementation UserFollowerTableHandler

@synthesize homepageRootController = _homepageRootController;
@synthesize userID = _userID;
@synthesize followerList = _followerList;

#pragma mark - Implementation of UITableViewDataSource

- (id)init {
    
    self = [super init];
    if (self != nil) {
        
        self.followerList = [NSMutableArray array];
        
        CollectionDataManager* sharedDataManager = [CollectionDataManager sharedInstance];
        long long userIDArr[] = {2ll, 4ll};
        for (int i = 0; i < sizeof(userIDArr) / sizeof(long long); i++) {
            UserInfo* user = [sharedDataManager cacheUserByUserID:userIDArr[i]];
            if (user != nil) {
                [self.followerList addObject:user];
            }
        }

    }
    
    return self;
}

- (void)dealloc {
    self.homepageRootController = nil;
    self.followerList = nil;
    [super dealloc];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.followerList != nil) {
        return [self.followerList count];
    }
    else {
        return 0;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString* followeeCellID = @"UserRelationCellID";
    
    UserRelationCell* cell = (UserRelationCell*)[tableView dequeueReusableCellWithIdentifier:followeeCellID];
    if (cell == nil) {
        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"UserRelationCell" owner:self options:nil];
        cell = [views objectAtIndex:0];
    }
    
    UserInfo* user = [self.followerList objectAtIndex:indexPath.row];
    cell.avatarImageView.image = user.avatar;
    cell.nicknameLabel.text = user.nickname;
    cell.signatureLabel.text = user.signature;
    [cell.actionButton setTitle:@"关注" forState:UIControlStateNormal];
    
    return cell;
}

#pragma mark - Implementation of UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.homepageRootController != nil && 
        self.homepageRootController.navigationController != nil) {
        UserProfileController* userProfileController = [[UserProfileController alloc] initWithNibName:@"UserProfileController" bundle:nil];
        UserInfo* user = [self.followerList objectAtIndex:indexPath.row];
        userProfileController.userID = user.userID;
        [self.homepageRootController.navigationController pushViewController:userProfileController animated:YES];
        [userProfileController release];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
